package com.example.exercise.MysqlUtil;

import android.content.SharedPreferences;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Mysqlconnect {

    private static String driver = "com.mysql.jdbc.Driver";//MySQL 驱动
    public static String url="jdbc:mysql://47.111.53.113:3306/exercise?useUnicode=true&useSSL=true";
    public static String url1 = "jdbc:mysql://146.56.130.237:3306/exercise?useUnicode=true&useSSL=false";
    private static String user = "exercise";//用户名
    private static String password = "Shsmyxx12310";//密码
    static Connection conn = null;
    private static PreparedStatement ps=null;//操作整合sql语句的对象

    private static Mysqlconnect mysqlconnect;
    public static Mysqlconnect get(){
        if (mysqlconnect == null){
            mysqlconnect = new Mysqlconnect();
        }
        return mysqlconnect;
    }


    public static Connection getConn(){

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static Connection getConnection() {
        try{
            Class.forName(driver).newInstance();
            conn=(Connection) DriverManager.getConnection(url,user,password);
            return conn;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }


    public static void closeAll(Connection conn, PreparedStatement ps){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}


